package com.zhongyu.consumer.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhongyu.consumer.service.WeatherClient;
import com.zhongyu.consumer.vo.RespData;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@RequestMapping("/weather")
public class WeatherController {

    @Resource
    private WeatherClient weatherClient;

    @Resource
    private RestTemplateBuilder restTemplateBuilder;


    @GetMapping("/query/{cityName}")
    public String queryWeather(@PathVariable String cityName){
        return weatherClient.searchWeather(cityName);
    }

    @GetMapping("/query2/{cityName}")
    public RespData query2(@PathVariable String cityName) throws JsonProcessingException {
        String url="http://wthrcdn.etouch.cn/weather_mini?city="+cityName;
        String str=restTemplateBuilder.build().getForObject(url,String.class);
        ObjectMapper objectMapper = new ObjectMapper();
        RespData respData=objectMapper.readValue(str,RespData.class);
        return  respData;
    }


}
